<template>
	<div class="sys-pos-container">
		<el-card shadow="hover">
			<el-form :model="state.queryParams" ref="queryForm" :inline="true">
				<el-form-item :label-width='70' label="客户">
					<el-select :clearable='true' class='ml10' v-model="state.queryParams.customerId" placeholder="请选择"
						filterable>
						<el-option v-for='item in state.customerDict' :key='item.value' :label='item.label'
							:value='item.value' />
					</el-select>
				</el-form-item>
				<el-form-item :label-width='70' label="Pid" prop="pid">
					<el-input placeholder="Pid" clearable class='ml10' @keyup.enter="handleQuery"
						v-model="state.queryParams.pid" />
				</el-form-item>
				<el-form-item :label-width='70' label='省份'>
					<el-select :clearable='true' class='ml10' v-model='state.queryParams.province' placeholder='请选择'
						filterable>
						<el-option v-for='item in optionDict.provinceDict' :key='item.value' :label='item.label'
							:value='item.value' />
					</el-select>
				</el-form-item>
				<el-form-item :label-width='70' label="产品名称" prop="productName">
					<el-input class='ml10' placeholder="产品名称" clearable @keyup.enter="handleQuery"
						v-model="state.queryParams.productName" />
				</el-form-item>
				<el-form-item :label-width='70' label="状态">
					<el-select class='ml10' v-model="state.queryParams.status" placeholder="请选择" filterable
						:clearable='true'>
						<el-option v-for='item in optionDict.status' :key='item.value' :label='item.label'
							:value='item.value' />
					</el-select>
				</el-form-item>
				<el-form-item :label-width='70' label="运营商">
					<el-select class='ml10' v-model="state.queryParams.platformId" placeholder="请选择" filterable
						:clearable='true'>
						<el-option v-for='item in optionDict.operatorPlatformDict' :key='item.value' :label='item.label'
							:value='item.value' />
					</el-select>
				</el-form-item>
				<el-form-item>
					<el-button-group>
						<el-button type="primary" icon="ele-Search" @click="handleQuery"
							v-auth="'diToucherProductPlan:query'"> 查询 </el-button>
						<el-button icon="ele-Refresh" @click="resetQuery" v-auth="'diToucherProductPlan:query'"> 重置
						</el-button>
					</el-button-group>
				</el-form-item>
				<el-form-item>
					<el-button-group>
						<el-button type="success" plain icon="ele-Plus" @click="openAdd" v-auth="'diToucherProductPlan:add'">
							新增
						</el-button>
						<el-button type="primary" icon="ele-Edit" @click="openEditBatch" v-auth="'diProductPlan:add'">
							批量编辑
						</el-button>
					</el-button-group>
				</el-form-item>
			</el-form>
		</el-card>

		<el-card shadow="hover" style="margin-top: 8px">
			<el-table :data="state.data" style="width: 100%" v-loading="state.loading" border stripe
				highlight-current-row @selection-change="editAggregation">
				<el-table-column type="selection" width="55" />
				<el-table-column header-align='center' width="55" type="index" label="序号" align="center" />

				<el-table-column header-align='center' prop="pid" label="Pid" width="200" show-overflow-tooltip />
				<el-table-column header-align='center' prop="customerName" label="客户名称" width="240"
					show-overflow-tooltip />
				<el-table-column header-align='center' prop="productName" label="产品名称" min-width="240"
					show-overflow-tooltip />
				<el-table-column header-align='center' width="80" label="运营商" align="center" show-overflow-tooltip>
					<template #default="scope">
						<el-tag type="success" v-if="scope.row.platformId === 1">移动</el-tag>
						<el-tag type="warning" v-else-if="scope.row.platformId === 2">联通</el-tag>
						<el-tag type="info" v-else-if="scope.row.platformId === 3">电信</el-tag>
						<el-tag type="error" v-else-if="scope.row.platformId === 4">广电</el-tag>
						<el-tag type="success" v-else-if="scope.row.platformId === 5">移动虚拟运营商</el-tag>
						<el-tag type="warning" v-else-if="scope.row.platformId === 6">联通虚拟运营商</el-tag>
						<el-tag type="info" v-else-if="scope.row.platformId === 7">电信虚拟运营商</el-tag>
						<el-tag type="error" v-else>未知</el-tag>
					</template>
				</el-table-column>
				<el-table-column header-align='center' prop='province' label="开通省份" width="82" align="center"
					show-overflow-tooltip />
				<el-table-column header-align='center' prop="city" label="屏蔽城市" width="82" show-overflow-tooltip />
				<el-table-column header-align='center' prop="sort" label="排序" align='right' width="82"
					show-overflow-tooltip />
				<el-table-column header-align='center' prop="weight" label="权重" align='right' width="82"
					show-overflow-tooltip />
				<el-table-column header-align='center' prop="settlementPrice" label="结算费(元)" align='right' width="100"
					show-overflow-tooltip>
					<template #default="scope">
						{{ (scope.row.settlementPrice / 100.00).toFixed(2) }}
					</template>
				</el-table-column>
				<el-table-column header-align='center' prop="dailyStartHour" label="起始" width="82" align="right"
					show-overflow-tooltip />
				<el-table-column header-align='center' prop="dailyEndHour" label="结束" width="82" align="right"
					show-overflow-tooltip />
				<el-table-column header-align='center' prop="requestLimit" label="请求上限" width="82" align='right'
					show-overflow-tooltip />
				<el-table-column header-align='center' prop="userLimit" label="用户上限" width="82" align='right'
					show-overflow-tooltip />
				<el-table-column header-align='center' prop="controlLevel" label="管控级别" width="82" align='right'
					show-overflow-tooltip />

				<el-table-column header-align='center' label="状态" align="center" show-overflow-tooltip width="82">
					<template #default="scope">
						<div @click="changeStatus(scope.row)">
							<el-switch v-model="scope.row.status" :active-value="1" :inactive-value="2" size="small"
								style="--el-switch-on-color: #67C23A;" v-if="scope.row.status <= 2"
								:style="+scope.row.status > 2 ? '--el-switch-off-color: #E6A23C' : '--el-switch-off-color: red'"
								v-auth="'diCustomerProduct:update'" />
							<el-switch v-model="scope.row.status" :active-value="1" :inactive-value="4" size="small"
								style="--el-switch-on-color: #67C23A;--el-switch-off-color: #E6A23C"
								v-auth="'diCustomerProduct:update'" v-else />
						</div>
					</template>
				</el-table-column>


				<el-table-column header-align='center' label="媒体反馈" width="82" align="center" show-overflow-tooltip>
					<template #default="scope">
						<el-tag type="success" v-if="scope.row.isMediaFeedback">是</el-tag>
						<el-tag type="info" v-else>否</el-tag>
					</template>
				</el-table-column>
				<el-table-column header-align='center' label="操作" width="220" fixed="right" align="center"
					show-overflow-tooltip>
					<template #default="scope">
						<el-button icon="ele-Edit" size="small" text type="primary"
							@click="openEdit(scope.row, 'update')" v-auth="'diToucherProductPlan:update'"> 编辑
						</el-button>
						<el-button icon="ele-Delete" size="small" text type="danger" @click="del(scope.row)"
							v-auth="'diToucherProductPlan:delete'"> 删除 </el-button>
						<el-button icon="ele-Edit" size="small" text type="primary" @click="openEdit(scope.row, 'ceshi')"
							v-auth="'diToucherProductPlan:update'"> 测试 </el-button>
					</template>
				</el-table-column>
			</el-table>
			<el-pagination @size-change="onHandleSizeChange" @current-change="onHandleCurrentChange" class="mt15"
				:page-sizes="[20, 50, 100]" v-model:current-page="state.queryParams.pageNum" background
				v-model:page-size="state.queryParams.pageSize" layout="total, sizes, prev, pager, next, jumper"
				:total="state.total">
			</el-pagination>
		</el-card>
		<AddDiToucherProductPlan ref="addDiToucherProductPlanRef" :title="state.editTitle" />
		<EditDiToucherProductPlan ref="editDiToucherProductPlanRef" :title="state.editTitle" />
		<EditBatchDiToucherProductPlan ref="editBatchDiToucherProductPlanRef" :title="state.editTitle" />
		<EditSynchronizationStatus ref="editSynchronizationStatusRef" :title="state.editTitle"
			@editStatus="handleQuery" />

	</div>
</template>

<script lang="ts" setup name="diToucherProductPlan">
import { onMounted, onUnmounted, reactive, ref } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';
import mittBus from '/@/utils/mitt';
import { optionDict } from '/@/const/dict';
import AddDiToucherProductPlan from '/@/views/delivery/operate/toucherProductPlan/component/addDiToucherProductPlan.vue';
import EditDiToucherProductPlan from '/@/views/delivery/operate/toucherProductPlan/component/editDiToucherProductPlan.vue';
import EditBatchDiToucherProductPlan from '/@/views/delivery/operate/toucherProductPlan/component/editBatchDiToucherProductPlan.vue';
import SysDiCustomerApi from '/@/api/delivery/customer';
import EditSynchronizationStatus from '/@/views/delivery/operate/toucherProductPlan/component/editSynchronizationStatus.vue';
import { DiToucherProductPlan } from '/@/api/models';
import SysDiToucherProductPlanApi from '/@/api/delivery/toucherProductPlan';

const addDiToucherProductPlanRef = ref<InstanceType<typeof AddDiToucherProductPlan>>();
const editDiToucherProductPlanRef = ref<InstanceType<typeof EditDiToucherProductPlan>>();
const editBatchDiToucherProductPlanRef = ref<InstanceType<typeof EditBatchDiToucherProductPlan>>();
const editSynchronizationStatusRef = ref<InstanceType<typeof EditSynchronizationStatus>>();

const state = reactive({
	loading: false,
	data: [] as Array<DiToucherProductPlan>,
	total: 0 as any,
	queryParams: {
		productName: undefined,
		province: undefined,
		pid: undefined,
		status: undefined,
		customerId: undefined,
		pageNum: 1,
		pageSize: 20,
		platformId: undefined
	},
	editTitle: '',
	batchModify: [],
	customerDict: [],
});

onMounted(async () => {
	mittBus.on('submitRefresh', () => {
		handleQuery();
	});
	await customerDictQuery();
});

onUnmounted(() => {
	mittBus.off('submitRefresh');
});

// 查询操作
const handleQuery = async () => {
	state.loading = true;
	if (!state.queryParams.province)
		state.queryParams.province = undefined;
	if (state.queryParams.status === '')
		delete state.queryParams.status;
	else if (state.queryParams.platformId === '')
		delete state.queryParams.platformId;

	const res = await SysDiToucherProductPlanApi().queryRecord(state.queryParams);
	state.data = res.data.result.items ?? [];
	state.total = res.data.result?.total;
	state.loading = false;
};

// 业务列表
const customerDictQuery = async () => {
	await SysDiCustomerApi().queryAll({}).then((res) => {
		if (res.data.resCode === '0000')
			state.customerDict = res.data.result ?? [];
	});
};

// 重置操作
const resetQuery = () => {
	state.queryParams.pid = undefined;
	state.queryParams.productName = undefined;
	state.queryParams.province = undefined;
	handleQuery();
};

// 打开新增页面
const openAdd = () => {
	console.log(1);

	state.editTitle = '添加';
	addDiToucherProductPlanRef.value?.openDialog({ sort: 100, weight: 100, syncPlanDaily: 1 });
};

// 打开编辑页面
const openEdit = (row: DiToucherProductPlan, data) => {
	if (data === 'update') {
		state.editTitle = '编辑';
		editDiToucherProductPlanRef.value?.openDialog(row, data);
	} else {
		state.editTitle = '测试';
		editDiToucherProductPlanRef.value?.openDialog(row, data);
	}

};

// 删除
const del = (row: any) => {
	ElMessageBox.confirm(`确定删除：【${row.productName} - ${row.pid} - ${row.province}】?`, '提示', {
		confirmButtonText: '确定',
		cancelButtonText: '取消',
		type: 'warning',
	})
		.then(async () => {
			let res = await SysDiToucherProductPlanApi().deletePost({ id: row.id });
			if (res.data.resCode != '0000')
				ElMessage.error(res.data.resMessage);
			else
				ElMessage.success("删除成功");
			await handleQuery();
		})
		.catch(() => { });
};

// 分页改变
const onHandleSizeChange = (val: number) => {
	state.queryParams.pageSize = val;
	handleQuery();
};

// 分页改变
const onHandleCurrentChange = (val: number) => {
	state.queryParams.pageNum = val;
	handleQuery();
};

// 选中的参数赋值给 batchModify
const editAggregation = (list: any) => {
	state.batchModify = list;
};

// 批量修改
const openEditBatch = () => {
	if (state.batchModify.length == 0) return ElMessage({
		message: `最少选中一列数据`,
		type: "error",
	});
	state.editTitle = '批量修改';
	editBatchDiToucherProductPlanRef.value?.openDialogedit({}, state.batchModify);
}


// 修改状态
const changeStatus = (row: any) => {
	state.editTitle = '提示';
	editSynchronizationStatusRef.value?.openDialog(row);
};

</script>
